package ch.fhnw.qualiscan.application.views;

import java.util.Vector;

import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.controllers.NewAccount;
import ch.fhnw.qualiscan.core.View;
import ch.fhnw.qualiscan.core.db.fieldfactories.NewUserFieldFactory;
import ch.fhnw.qualiscan.core.db.pojos.User;
import ch.fhnw.qualiscan.core.helpers.EmailHelper;
import ch.fhnw.qualiscan.core.helpers.UserSession;
import ch.fhnw.qualiscan.core.libraries.NotificationLibrary;
import eu.livotov.tpt.i18n.TM;

public class NewAccountView extends View {
	
	private Form signUpForm;
	private NewAccount newAccountController;
	
	
	
	@Override
	public void initComponents() {
		
	}
	
	public NewAccountView(NewAccount controller) {
		newAccountController = controller;
	}
	
	@Override
	public void buildLayout() {
		this.removeAllComponents();
		
		if(newAccountController.userSessionHelper.isUserSessionSet()) {
			addComponent(textHelper.h1(TM.get("newaccount.title2")));
			addComponent(textHelper.p(TM.get("newaccount.desc2")));
			
		} else {
			addComponent(textHelper.h1(TM.get("newaccount.title")));
			addComponent(textHelper.p(TM.get("newaccount.desc")));

			addComponent(buildSignUpForm());
		}
	}
	
	/**
	 * Builds the form for creating a new account.
	 * @return Form
	 */
	public Form buildSignUpForm() {
		final User user;
		boolean isStandalone;

		if(newAccountController.getInvitationSurvey() != null) {
			user = newAccountController.getUser();
			isStandalone = false;
		} else {
			user = new User();
			isStandalone = true;
		}

		
		
		signUpForm = new Form();
        
        BeanItem<User> item = new BeanItem<User>(user);
        
        signUpForm.setItemDataSource(item);
        signUpForm.setImmediate(true);
        signUpForm.setFormFieldFactory(new NewUserFieldFactory(isStandalone));
        
        Vector<String> order = new Vector<String>();
        order.add("firstname");
        order.add("lastname");
        order.add("email");
        order.add("password");
        order.add("language");
		
        signUpForm.setVisibleItemProperties(order); 
        
        HorizontalLayout buttonBar = new HorizontalLayout();
        signUpForm.getFooter().addComponent(buttonBar);
        Button commit = new Button(TM.get("action.save"), new ClickListener() {
            public void buttonClick(ClickEvent event) {
            	try {
            		signUpForm.commit();
            		if (newAccountController.getInvitationSurvey() != null) {
            			newAccountController.updateUser(user);
            		}
            		else {
            			if(newAccountController.saveUser(user)) {
                			
                		}
                		else {
                			NotificationLibrary.showError(TM.get("notification.error.username.exists"), null);
                			return;
                		}
            		}
            		

            		// changing language interface if necessary
            		TM.getDictionary().setDefaultLanguage(user.getLanguage().getLocale());
            		requestRepaintAll();
            		
            		// show notifications
            		NotificationLibrary.showSuccess(TM.get("action.saved"), null);
            		
            		// redirect
            		newAccountController.getAppData().getLoaderLib().setUrlFragment(0, "Start");	
            		
            	}
            	catch (InvalidValueException e) {
                    return;
            	}
            }
        });
 
        commit.setIcon(Config.ICON_SAVE);
        buttonBar.addComponent(commit);
        
		return signUpForm;
	}

	@Override
	public void reBuild() {
		// TODO Auto-generated method stub
		
	}

}
